package com.flow.asyncflow.core.entity.core;

import com.baomidou.mybatisplus.annotation.TableField;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;
import lombok.AccessLevel;
import lombok.Data;
import lombok.Setter;

import java.util.HashMap;
import java.util.Map;

/**
 * @description: 实体基类
 * @author: lgq07612
 * @create: 2020-08-08 11:12
 **/
@Data
public class BaseEntity<T> {
    @TableField(exist = false)
    @Setter(AccessLevel.PRIVATE)
    public Map<String, Object> changeValues;
    @TableField(exist = false)
    @Setter(AccessLevel.PRIVATE)
    // public Table<String, Object, Object> changeValuesV2 = HashBasedTable.create();
    public Map<String, Map<Object, Object>> changeValuesV2;

    public BaseEntity() {
        changeValues = new HashMap<>();
        changeValuesV2 = new HashMap<>();
    }

    /**
     * @param col     属性名称
     * @param new_val 新值
     * @param old_val 老的值
     */
    protected void ChangeStack(String col, Object new_val, Object old_val) {

        //新的不能为null,不允许将属性更新为null
        //老的不能为null,老的是null时，当前实体肯定是new出来的要进行插入的，直接走反射去拿变化的属性
        if (new_val == null || old_val == null) {
//            if (old_val == null) {
//                Map<Object, Object> changeValue = new HashMap<>();
//                changeValue.put(new_val, null);
//                changeValuesV2.put(col, changeValue);
//            }
            return;
        }

        if (new_val.equals(old_val)) {
            return;
        }
        //不存在这个字段 或者 存在并且不相等(改变了)
        if (!changeValues.containsKey(col) || !changeValues.get(col).equals(new_val)) {
            changeValues.put(col, new_val);
            Map<Object, Object> changeValue = new HashMap<>();
            changeValue.put(new_val, old_val);
            changeValuesV2.put(col, changeValue);
        }


    }
}
